package com.handler.http.message.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Map;

/**
 * @description:
 * @author: hhj
 * @time: 2020-07-08 15:15
 */

public class ObjectUtil {

    private static Logger logger = LoggerFactory.getLogger(ObjectUtil.class);

    /**
     * 判断一个对象是否为null或empty对象
     * empty对象包括空字符串(包括空格串)，空数组,字集合等
     */
    public static <T> boolean isEmpty(T t) {
        //null对象
        if (t == null) {
            return true;
        }
        //序列串
        if (t instanceof CharSequence) {
            CharSequence cs = (CharSequence) t;
            for (int i = 0; i < cs.length(); i++) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }
            return true;
        }
        //数组
        if (t.getClass().isArray()) {
            return Array.getLength(t) == 0;
        }
        //集合
        if (t instanceof Collection) {
            return ((Collection) t).isEmpty();
        }
        //map
        if (t instanceof Map) {
            return ((Map) t).isEmpty();
        }
        //默认为false,即非null
        return false;
    }

    /**
     * 判断一个对象在业务逻辑上是否是null
     * 包括 isNullOrEmpty判断，以及数字0, 空值数组，空值集合等
     */
    public static <T> boolean isEmptyOrZero(T t) {
        if (isEmpty(t)) {
            return true;
        }

        //数字
        if (t instanceof Number) {
            return Double.doubleToRawLongBits(((Number) t).doubleValue()) == 0;
        }

        //数组
        if (t.getClass().isArray()) {
            for (int i = 0, j = Array.getLength(t); i < j; i++) {
                Object object = Array.get(t, i);
                if (!isEmptyOrZero(object)) {
                    return false;
                }
            }

            return true;
        }

        //空值集合
        if (t instanceof Collection) {
            for (Object obj : (Collection) t) {
                if (!isEmptyOrZero(obj)) {
                    return false;
                }
            }

            return true;
        }

        return false;
    }

    /**
     * ModelMap转 object
     *
     * @param modelMap
     * @param beanClass
     * @return
     * @throws Exception
     */
    public static Object mapToObject(ModelMap modelMap, Class<?> beanClass) throws Exception {

        if (null == modelMap) {
            return null;
        }

        try {
            Object obj = beanClass.newInstance();
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                int mod = field.getModifiers();
                if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                    continue;
                }

                field.setAccessible(true);
                field.set(obj, modelMap.get(field.getName()));
            }
            return obj;
        } catch (Exception e) {
            logger.error("数据转换出错:{}", e.getMessage());
            throw new Exception(e.getMessage());
        }

    }

}
